package com.asion.vmovie.activities;

import android.animation.Animator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;

import com.asion.vmovie.R;
import com.asion.vmovie.fragment.HomeFragment;
import com.asion.vmovie.fragment.MineFragment;
import com.asion.vmovie.fragment.MuHouFragment;
import com.asion.vmovie.fragment.XiLieFragment;
import com.asion.vmovie.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.activity_main)
    FrameLayout activityMain;

    @BindView(R.id.nav_bottom)
    BottomNavigationView bottomNavigationView;

    private HomeFragment homeFragment;
    private Fragment lastFragment;

    public static void showAction(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                R.anim.fade_out);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);

        homeFragment = new HomeFragment();

        changeFragment(homeFragment);

        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_home:
                changeFragment(homeFragment);
                break;
            case R.id.nav_series:
                changeFragment(XiLieFragment.newInstance());
                break;
            case R.id.nav_behind:
                changeFragment(MuHouFragment.newInstance());
                break;
            case R.id.nav_mine:
                changeFragment(MineFragment.newInstance());
                break;
        }
        return true;
    }

    private void changeFragment(Fragment fragment){
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            if (lastFragment != null) {
                fragmentTransaction.hide(lastFragment);
            }
            fragmentTransaction.add(R.id.activity_main,fragment).commit();
        }else{
            fragmentTransaction.hide(lastFragment).show(fragment).commit();
        }
        lastFragment = fragment;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void closeEventBus(Object message){
        if( message instanceof String && "show".equals(message)){
            show(bottomNavigationView);
        }else if(message instanceof String && "hide".equals(message)){
           hide(bottomNavigationView);
        }else if(message instanceof Bundle){
            PingDaoDetialActivity.showAction(this, (Bundle) message);
        }else if(message instanceof String && "search".equals(message)){
            SearchActivity.showAction(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private List<Long> longs = new ArrayList<>();

    @Override
    public void onBackPressed() {
        longs.add(System.currentTimeMillis());
        ToastUtil.showShort("再按一次退出");
        if(longs.size()==2 && longs.get(1) - longs.get(0)<=2000){
            this.finish();
        }
        if(longs.size()==2){
            longs.clear();
        }
    }

    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();

    private void hide(final View view) {
        ViewPropertyAnimator animator = view.animate().translationY(view.getHeight()).setInterpolator(INTERPOLATOR).setDuration(200);
        animator.setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                show(view);
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animator.start();
    }


    private void show(final View view) {
        ViewPropertyAnimator animator = view.animate().translationY(0).setInterpolator(INTERPOLATOR).setDuration(200);
        animator.setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                hide(view);
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animator.start();

    }
}
